package com.shanghaionstar.nav.presenter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.map.common.DriveSegmentInfo;
import com.onstar.cn.map.common.PoiItemInfo;
import com.shanghaionstar.R;
import com.shanghaionstar.nav.iview.INavRoutePlanDetailView;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.utils.MapHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 6335yz on 2016/7/7.
 */
public class NavRoutePlanDetailPresenter extends BasePresenter implements INavRoutePlanDetailView {
    private AbstractMapActivity mapActivity;
    private PoiItemInfo routeEndPoiItemInfo;
    private PoiItemInfo routeStartPoiItemInfo;
    private String timeInfo;
    private String disInfo;

    private MapHandler mapHandler;

    public NavRoutePlanDetailPresenter(IMVPScheduler mvpScheduler, AbstractMapActivity mapActivity) {
        super(mvpScheduler);
        this.mapActivity = mapActivity;
        mapHandler = mapActivity.getRouteMapHandler();
    }

//    public static NavRoutePlanDetailFragment newInstance(PoiItemInfo routeStartPoiItemInfo, PoiItemInfo routeEndPoiItemInfo, String timeInfo, String disInfo) {
//        NavRoutePlanDetailFragment fragment = new NavRoutePlanDetailFragment();
//        Bundle bundle = new Bundle();
//        bundle.putSerializable("routeStartPoiItemInfo", routeStartPoiItemInfo);
//        bundle.putSerializable("routeEndPoiItemInfo", routeEndPoiItemInfo);
//        bundle.putString("timeInfo", timeInfo);
//        bundle.putString("disInfo", disInfo);
//        fragment.setArguments(bundle);
//        return fragment;
//    }

    public void getData(Bundle bun, TextView timeTv, TextView spaceTv) {
        if (bun != null) {
            routeStartPoiItemInfo = (PoiItemInfo) bun.getSerializable("routeStartPoiItemInfo");
            routeEndPoiItemInfo = (PoiItemInfo) bun.getSerializable("routeEndPoiItemInfo");
            String timeInfo = bun.getString("timeInfo");
            String disInfo = bun.getString("disInfo");
            timeTv.setText(timeInfo == null ? "" : timeInfo);
            spaceTv.setText(disInfo == null ? "" : disInfo);
        }
    }

    public void initData(ListView routeLv) {
        List<DriveSegmentInfo> list = new ArrayList<DriveSegmentInfo>();
        DriveSegmentInfo start = new DriveSegmentInfo();
        start.setStepedDescription(
                mapActivity.getString(R.string.poi_route_start_hint_str_s) + " (" + routeStartPoiItemInfo.getTitle() + ")");
        DriveSegmentInfo end = new DriveSegmentInfo();
        end.setStepedDescription(
                mapActivity.getString(R.string.poi_route_end_hint_str_s) + " (" + routeEndPoiItemInfo.getTitle() + ")");
        list.add(start);
        if(mapHandler.getSegmentInfoList()!=null) {
            list.addAll(1, mapHandler.getSegmentInfoList());
        }
//            ((NavActivity) mContext).getMapHandler().getSegmentInfoList();
//        list.addAll(1, PoiLocationInfoInstance.getInstance().getDriveSegmentInfoList());
        list.add(end);
        if (view instanceof INavRoutePlanDetailView) {
            ((INavRoutePlanDetailView) view).setAdapter(list);
        }

    }


    @Override
    public void setAdapter(List<DriveSegmentInfo> list) {

    }

    @Override
    public void setLoading(boolean isLoading) {

    }

    @Override
    public void onResponseError(OnStarException e) {

    }

    @Override
    public void onReponseSuccess(Object object) {

    }

    @Override
    public Activity onActivity() {
        return null;
    }
}
